package cn.edu.nuc.crm.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.edu.nuc.crm.dto.PageInfo;
import cn.edu.nuc.crm.entity.Product;
import cn.edu.nuc.crm.service.interfaces.ProductService;

@Controller
public class ProductController {
	@Autowired
	private ProductService productService;
	
	/**
	 * 列出产品列表
	 * @param current
	 * @param session
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/productlist",method=RequestMethod.GET)
	public String select(@RequestParam(name="current",defaultValue="1") int current,
			HttpSession session, Model model){	
			List<Product> product = new ArrayList<Product>();
			product = productService.product();
			session.setAttribute("product", product);
			System.out.println(product);
			PageInfo pi = productService.pi(current);
			System.out.println(pi);
			session.setAttribute("pi", pi);	
		
			return "productlist"; 
	}
	/**
	 * 查询产品
	 * @return
	 */
	@RequestMapping(value="/productsearch",method=RequestMethod.POST)
	public String search(Model model,String productname){
		System.out.println(productname);
		if( productname != null && !productname.equals("") ){
			List<Product> list = productService.selectByName(productname);
			model.addAttribute("list", list);
			return "/product/search";
		}
		return "redirect:productlist";	
	}
	/**
	 * 增加产品
	 * @return
	 */
	@RequestMapping(value="product/add",method=RequestMethod.GET)
	public  String add(){
		return "product/add";		
	}
	@RequestMapping(value="productadd",method=RequestMethod.POST)
	public String add(Product product,HttpServletRequest request){
		try{
		productService.insert(product);
		return "productlist";
		}catch(Exception e){
		  request.setAttribute("msg", "添加失败");
		  return "product/add";
		}
	}
	/**
	 * 删除产品
	 * @param productid
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/productdelete",method=RequestMethod.GET)
	public String delete(Integer productid,HttpSession session){
		int result=productService.deleteByPrimaryKey(productid);
		if(result>0){	
			return "/success1";	
		}else{
			session.setAttribute("info", "该产品不能删除");
			return "/productlist";
		}
	}
	/**
	 * 编辑
	 * @param productid
	 * @param session
	 * @return
	 */
	
	 @RequestMapping(value="/product/edit",method=RequestMethod.GET)
	public String update(Integer productid,HttpSession session){
		Product product=productService.selectByPrimaryKey(productid);
		System.out.println(product);
		session.setAttribute("item",product);
		return "product/edit";
		
	}
	@RequestMapping(value="/productedit",method=RequestMethod.POST)
	public String update(HttpServletRequest request,Product product){
	try{
		productService.updateByPrimaryKey(product);
		return "redirect:productlist";
		}catch(Exception e){
			request.setAttribute("msg", "修改失败");
			return "product/edit";
		}	
	}
}
